<template>
  <el-dialog v-loading="loading" title="分配角色" :visible="showRoleDialog" @close="closeDialog" @open="dialogOpen">
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <div style="text-align: right">
        <el-button type="primary" @click="clickSubmit">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { reqGetRoleList } from '@/api/setting'
import { reqGetUserDetailById } from '@/api/user'
import { reqAssignRoles } from '@/api/employee'
export default {
  name: 'AssignRole',
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [],
      checkList: [],
      loading: false
    }
  },
  // created() {
  //   this.getRoleList()
  //   this.getUserDetailById()
  // },
  methods: {
    closeDialog() {
      this.$emit('update:showRoleDialog', false)
      this.checkList = []
    },
    dialogOpen() {
      this.loading = true
      Promise.all([this.getRoleList(), this.getUserDetailById()]).then(() => {
        this.loading = false
      })
    },
    async getRoleList() {
      const { data: { rows }} = await reqGetRoleList(1, 100)
      this.list = rows
    },
    async getUserDetailById() {
      const { data: { roleIds }} = await reqGetUserDetailById(this.userId)
      this.checkList = roleIds
    },
    async clickSubmit() {
      await reqAssignRoles({
        id: this.userId,
        roleIds: this.checkList
      })
      this.$message.success('添加用户权限完成')
      this.closeDialog()
    }
  }
}
</script>
<style lang="scss" scoped>
.assign-role {
  ::v-deep {
    .el-checkbox {
      font-size: 30px;
    }
  }
}
</style>
